﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="DoubleResidualStopCriterion.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra
{
    #region Usings

    using System;

    #endregion

    /// <summary>
    ///     Provides methods to determine the end of an iteration process using residual.
    /// </summary>
    public sealed class DoubleResidualStopCriterion : IterationsStopCriterion<double>
    {
        #region Constructors and Destructors

        #region public

        /// <summary>
        ///     Initializes a new instance of the <see cref="DoubleResidualStopCriterion"/> class.
        /// </summary>
        /// <param name="requiredResidual">
        ///     The required residual.
        /// </param>
        public DoubleResidualStopCriterion(double requiredResidual)
        {
            this.RequiredResidual = requiredResidual;
        }

        #endregion

        #endregion

        #region Properties

        #region public

        /// <summary>
        ///     Gets the required residual.
        /// </summary>
        /// <value>
        ///     The required residual.
        /// </value>
        public double RequiredResidual
        {
            get;
            private set;
        }

        #endregion

        #endregion

        #region Overridden Methods

        #region public

        /// <summary>
        ///     Determines whether an iteration process should be stopped.
        /// </summary>
        /// <param name="data">
        ///     The data describing a current state of the iteration process.
        /// </param>
        /// <returns>
        ///     <see langword="true"/> if the iteration process should be stopped; otherwise, <see langword="false"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="data"/> is <see langword="null"/>.</exception>
        public override bool ShouldStop(IterationData<double> data)
        {
            #if SAFE

            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            #endif

            return data.Residual <= this.RequiredResidual;
        }

        #endregion

        #endregion
    }
}